home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / screen-resolution-extra / policyui.py < prev    next >
Text File  |  2009-10-14  |  8KB  |  239 lines

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. ## Copyright (C) 2001-2008 Alberto Milone <albertomilone@alice.it>
  4.  
  5. ## This program is free software; you can redistribute it and/or modify
  6. ## it under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2 of the License, or
  8. ## (at your option) any later version.
  9.  
  10. ## This program is distributed in the hope that it will be useful,
  11. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ## GNU General Public License for more details.
  14.  
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with this program; if not, write to the Free Software
  17. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  
  20. import gtk, gobject, sys, dbus, logging
  21. import ScreenResolution
  22. from ScreenResolution import ui
  23.  
  24. SERVICE_NAME   = 'com.ubuntu.ScreenResolution.Mechanism'
  25. OBJECT_PATH    = '/'
  26. INTERFACE_NAME = 'com.ubuntu.ScreenResolution.Mechanism'
  27. usage = 'python policyui.py 1024x768'
  28.  
  29. import os
  30. import sys
  31. from subprocess import Popen, PIPE
  32.  
  33. import XKit
  34. from XKit import xutils, xorgparser
  35.  
  36. clean = False
  37.  
  38. def checkVirtual(virtres):
  39.     '''See if it's necessary to set the virtual resolution'''
  40.  
  41.     source = '/etc/X11/xorg.conf'
  42.     
  43.     try:
  44.         a = xutils.XUtils(source)
  45.     except(IOError, XKit.xorgparser.ParseException):#if xorg.conf is missing or broken
  46.         return False
  47.     
  48.     if len(a.globaldict['Screen']) > 0:
  49.         # See if the virtual resolution is already there and if it's big enough
  50.         res = None
  51.         for screen in a.globaldict['Screen']:
  52.             try:
  53.                 res = a.getValue('SubSection', 'virtual', 0, identifier='Display', sect="Screen", reference=None)
  54.                 if res:
  55.                     if 'x' in res.lower():
  56.                         res = res.lower().split('x')
  57.                     elif ' ' in res.lower().strip():
  58.                         res = res.lower().split(' ')
  59.                         res = filter(lambda x: x != '', res)
  60.                         if len(res) == 2 and int(virtres[0]) <= int(res[0]) and int(virtres[1]) <= int(res[1]):
  61.                             # Nothing to do, the virtual resolution is already there
  62.                             return True
  63.             except (XKit.xorgparser.SectionException, XKit.xorgparser.OptionException, AttributeError):
  64.                 pass
  65.     
  66.     return False
  67.  
  68. def get_xkit_service(widget=None):
  69.     '''
  70.     returns a dbus interface to the screenresolution mechanism
  71.     '''
  72.     service_object = dbus.SystemBus().get_object(SERVICE_NAME, OBJECT_PATH)
  73.     service = dbus.Interface(service_object, INTERFACE_NAME)
  74.  
  75.     return service
  76.  
  77. def gui_dialog(message, parent_dialog,
  78.                       message_type=None,
  79.                       widget=None, page=0, 
  80.                       broken_widget=None):
  81.     '''
  82.     Displays an error dialog.
  83.     '''
  84.     if message_type == 'error':
  85.         message_type = gtk.MESSAGE_ERROR
  86.         logging.error(message)
  87.     elif message_type == 'info':
  88.         message_type = gtk.MESSAGE_INFO
  89.         logging.info(message)
  90.  
  91.  
  92.     
  93.         
  94.     dialog = gtk.MessageDialog(parent_dialog,
  95.                                gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
  96.                                message_type, gtk.BUTTONS_OK,
  97.                                message)
  98.     
  99.     translation = ui.AbstractUI()
  100.     
  101.     dialog.set_title(translation.string_title)
  102.     
  103.     if widget != None:
  104.         if isinstance (widget, gtk.CList):
  105.             widget.select_row (page, 0)
  106.         elif isinstance (widget, gtk.Notebook):
  107.             widget.set_current_page (page)
  108.     if broken_widget != None:
  109.         broken_widget.grab_focus ()
  110.         if isinstance (broken_widget, gtk.Entry):
  111.             broken_widget.select_region (0, -1)
  112.  
  113.     if parent_dialog:
  114.         dialog.set_position (gtk.WIN_POS_CENTER_ON_PARENT)
  115.         dialog.set_transient_for(parent_dialog)
  116.     else:
  117.         dialog.set_position (gtk.WIN_POS_CENTER)
  118.  
  119.     ret = dialog.run ()
  120.     dialog.destroy()    
  121.  
  122.     return ret
  123.  
  124.  
  125.  
  126. class BootWindow:
  127.     optimal_virtual_resolution = ['2048', '2048']
  128.     
  129.     def __init__(self, resolution):
  130.         
  131.         translation = ui.AbstractUI()
  132.         self.permission_text = translation.string_permission_text
  133.         self.dbus_cant_connect = translation.string_dbus_cant_connect
  134.         self.operation_complete = translation.string_operation_complete
  135.         self.cant_apply_settings = translation.string_cant_apply_settings
  136.         
  137.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  138.         self.window.connect("delete_event", self.on_delete_event)
  139.         self.window.connect("destroy", self.on_destroy)
  140.         
  141.         self.window.set_border_width(20)
  142.         
  143.         self.window.set_title(translation.string_title)
  144.         self.window.set_position(gtk.WIN_POS_CENTER)
  145.         
  146.         gtk.window_set_default_icon_from_file("/usr/share/icons/hicolor/16x16/apps/gnome-display-properties.png")
  147.         
  148.         vbox = gtk.VBox(spacing=20)
  149.         self.resolution = resolution
  150.         self.label = gtk.Label(self.permission_text)
  151.         self.label.set_line_wrap(True)
  152.         self.label.set_justify(gtk.JUSTIFY_FILL)
  153.         self.button1 = gtk.Button(label=None, stock='gtk-yes', use_underline=False)
  154.         self.button1.connect("clicked", self.on_button1_clicked, None)
  155.         self.button1.show()
  156.         self.button2 = gtk.Button(label=None, stock='gtk-no', use_underline=False)
  157.         self.button2.connect("clicked", self.on_button2_clicked, None)
  158.         self.button2.show()
  159.         
  160.         buttonbox = gtk.HButtonBox()
  161.         buttonbox.set_layout(gtk.BUTTONBOX_END)
  162.         buttonbox.set_spacing(10)
  163.         buttonbox.pack_start(self.button2)
  164.         buttonbox.pack_start(self.button1)
  165.         buttonbox.show()
  166.         vbox.pack_start(self.label)
  167.         vbox.pack_start(buttonbox)
  168.         self.window.add(vbox)
  169.         self.label.show()
  170.         
  171.         vbox.show()
  172.     
  173.     def on_button1_clicked(self, widget, data=None):
  174.         self.window.hide()
  175.         self.conf = get_xkit_service()
  176.         if not self.conf:
  177. #            gui_dialog(self.dbus_cant_connect,
  178. #                              self.window, message_type='error')
  179.             sys.exit(1)
  180.         
  181.         # If the required resolution is lower than the optimal virtual
  182.         # resolution (usually the highest resolution which doesn't break
  183.         # direct rendering) then use the latter instead.
  184.         if int(self.resolution[0]) < int(self.optimal_virtual_resolution[0]) and \
  185.            int(self.resolution[1]) < int(self.optimal_virtual_resolution[1]):
  186.             self.resolution = self.optimal_virtual_resolution
  187.  
  188.         status = self.conf.setVirtual(self.resolution)
  189.         #print 'Status', status
  190.         if status == True:
  191.             #gui_dialog(self.operation_complete, self.window, message_type='info')
  192.             global clean
  193.             clean = True
  194.             gtk.main_quit()
  195.             #sys.exit(0)
  196.  
  197.         else:
  198.             #gui_dialog(self.cant_apply_settings, self.window, message_type='error')
  199.             #sys.exit(1)
  200.             gtk.main_quit()
  201.         
  202.     def on_button2_clicked(self, widget, data=None):
  203.         self.window.hide()
  204. #        gui_dialog(self.cant_apply_settings, self.window, message_type='error')
  205.         #sys.exit(1)
  206.         gtk.main_quit()
  207.         
  208.         
  209.     def on_delete_event(self, widget, event, data=None):
  210.         # Close the window:
  211.         return False
  212.  
  213.     def on_destroy(self, widget, data=None):
  214.         gtk.main_quit()
  215.  
  216.     def show(self):
  217.        self.window.show()
  218.  
  219.  
  220.  
  221.  
  222. if __name__ == "__main__":
  223.     if len(sys.argv) > 1:
  224.         for param in sys.argv[1:]:
  225.             if 'x' not in param:
  226.                 sys.exit(0)    
  227.     else:
  228.         sys.exit(0)
  229.     
  230.     res = sys.argv[1]
  231.     res = res.strip().split('x')
  232.     if checkVirtual(res):
  233.         sys.exit(0)
  234.     window = BootWindow(res)
  235.     window.show()
  236.     gtk.main()
  237.     if not clean:
  238.         sys.exit(1)
  239.